<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2006/12/29 13:46:14 $)
// $RCSfile: setup.php,v $ : $Revision: 1.11 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_staff.php");
require_once ("./includes/functions_departments.php");
require_once ("./includes/functions_users.php");

/**
* Called when the module is to be installed
*/
function _core_install($action, $page = '') {
	global $_SWIFT, $dbCore;
	
	if ($action == "getpages")
	{
		return "1";
	} else if ($action == "buildpage") {
		switch ($page)
		{
			case '1':
				$_SWIFT["query"]["create"]["registry"] = "CREATE TABLE ". TABLE_PREFIX ."registry(
																		vkey VARCHAR(50) NOT NULL DEFAULT '',
																		data MEDIUMTEXT,
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		isvolatile SMALLINT UNSIGNED NOT NULL DEFAULT '0',
																		datasize INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY (vkey)
																	);";
				
				$_SWIFT["query"]["create"]["settings"] = "CREATE TABLE ". TABLE_PREFIX ."settings(
																		settingid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		section VARCHAR(50) NOT NULL DEFAULT '',
																		vkey VARCHAR(50) NOT NULL DEFAULT '',
																		data MEDIUMTEXT,
																		PRIMARY KEY(settingid),
																		KEY sections (section, vkey)
																	);";

				$_SWIFT["query"]["create"]["settingsgroups"] = "CREATE TABLE ". TABLE_PREFIX ."settingsgroups(
																		sgroupid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		name VARCHAR(100) NOT NULL DEFAULT '',
																		module VARCHAR(100) NOT NULL DEFAULT '',
																		displayorder INT(5) NOT NULL DEFAULT '0',
																		ishidden INT(1) NOT NULL DEFAULT '0',
																		PRIMARY KEY(sgroupid)
																	);";

				$_SWIFT["query"]["create"]["settingsfields"] = "CREATE TABLE ". TABLE_PREFIX ."settingsfields(
																		sfieldid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		sgroupid INT UNSIGNED NOT NULL DEFAULT '0',
																		name VARCHAR(200) NOT NULL DEFAULT '',
																		customvalue MEDIUMTEXT,
																		iscustom INT(1) UNSIGNED NOT NULL DEFAULT '0',
																		settingtype VARCHAR(100) NOT NULL DEFAULT 'text',
																		module VARCHAR(100) NOT NULL DEFAULT '',
																		displayorder INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY (sfieldid)
																	);";

				$_SWIFT["query"]["create"]["sessions"] = "CREATE TABLE ". TABLE_PREFIX ."sessions(
																		sessionid VARCHAR(32) NOT NULL DEFAULT '',
																		ipaddress VARCHAR(30) NOT NULL DEFAULT '',
																		lastactivity INT UNSIGNED NOT NULL DEFAULT '0',
																		useragent VARCHAR(60) NOT NULL DEFAULT '0',
																		isloggedin SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		sessiontype INT UNSIGNED NOT NULL DEFAULT '0',
																		typeid INT UNSIGNED NOT NULL DEFAULT '0',
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		status SMALLINT NOT NULL DEFAULT '0',
																		captcha VARCHAR(10) NOT NULL DEFAULT '',
																		iswinapp SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY(sessionid),
																		KEY sessions1 (sessiontype, lastactivity, status),
																		KEY typeid (typeid)
																	) ENGINE = MEMORY;";

				$_SWIFT["query"]["create"]["signatures"] = "CREATE TABLE ". TABLE_PREFIX ."signatures(
																		signatureid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		staffid INT UNSIGNED NOT NULL DEFAULT '0',
																		signature MEDIUMTEXT,
																		PRIMARY KEY(signatureid),
																		KEY signatures1 (staffid)
																	);";

				$_SWIFT["query"]["create"]["staff"] = "CREATE TABLE ". TABLE_PREFIX ."staff(
																		staffid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		fullname VARCHAR(100) NOT NULL DEFAULT '',
																		username VARCHAR(30) NOT NULL DEFAULT '',
																		password VARCHAR(32) NOT NULL DEFAULT '',
																		staffgroupid INT UNSIGNED NOT NULL DEFAULT '0',
																		email VARCHAR(150) NOT NULL DEFAULT '',
																		mobilenumber VARCHAR(20) NOT NULL DEFAULT '',
																		lastvisit INT UNSIGNED NOT NULL DEFAULT '0',
																		lastvisit2 INT UNSIGNED NOT NULL DEFAULT '0',
																		lastactivity INT UNSIGNED NOT NULL DEFAULT '0',
																		timezoneoffset VARCHAR(10) NOT NULL DEFAULT '0',
																		enabledst SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		startofweek INT UNSIGNED NOT NULL DEFAULT '1',
																		pmunread INT UNSIGNED NOT NULL DEFAULT '0',
																		groupassigns SMALLINT(1) UNSIGNED NOT NULL DEFAULT '1',
																		enablepmalerts SMALLINT(1) UNSIGNED NOT NULL DEFAULT '1',
																		enablepmjsalerts SMALLINT(1) UNSIGNED NOT NULL DEFAULT '1',
																		ticketviewid INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY(staffid),
																		KEY staff1 (staffgroupid)
																	);";

				$_SWIFT["query"]["create"]["staffgroup"] = "CREATE TABLE ". TABLE_PREFIX ."staffgroup(
																		staffgroupid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		title VARCHAR(100) NOT NULL DEFAULT '',
																		isadmin SMALLINT(1) NOT NULL DEFAULT '0',
																		PRIMARY KEY(staffgroupid),
																		KEY staffgroup1 (isadmin)
																	);";

				$_SWIFT["query"]["create"]["staffgroupsettings"] = "CREATE TABLE ". TABLE_PREFIX ."staffgroupsettings(
																		sgroupsettingid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		staffgroupid INT UNSIGNED NOT NULL DEFAULT '0',
																		name VARCHAR(100) NOT NULL DEFAULT '',
																		value VARCHAR(255) NOT NULL DEFAULT '',
																		PRIMARY KEY (sgroupsettingid),
																		KEY staffgroupsettings1 (staffgroupid),
																		UNIQUE KEY (staffgroupid, name)
																	);";

		$_SWIFT["query"]["create"]["staffschedules"] = "CREATE TABLE ". TABLE_PREFIX ."staffschedules (
																		staffscheduleid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		staffid INT UNSIGNED NOT NULL DEFAULT '0',
																		sunday_enabled SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		sunday_open VARCHAR(6) NOT NULL DEFAULT '00:00',
																		sunday_close VARCHAR(6) NOT NULL DEFAULT '00:00',
																		monday_enabled SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		monday_open VARCHAR(6) NOT NULL DEFAULT '00:00',
																		monday_close VARCHAR(6) NOT NULL DEFAULT '00:00',
																		tuesday_enabled SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		tuesday_open VARCHAR(6) NOT NULL DEFAULT '00:00',
																		tuesday_close VARCHAR(6) NOT NULL DEFAULT '00:00',
																		wednesday_enabled SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		wednesday_open VARCHAR(6) NOT NULL DEFAULT '00:00',
																		wednesday_close VARCHAR(6) NOT NULL DEFAULT '00:00',
																		thursday_enabled SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		thursday_open VARCHAR(6) NOT NULL DEFAULT '00:00',
																		thursday_close VARCHAR(6) NOT NULL DEFAULT '00:00',
																		friday_enabled SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		friday_open VARCHAR(6) NOT NULL DEFAULT '00:00',
																		friday_close VARCHAR(6) NOT NULL DEFAULT '00:00',
																		saturday_enabled SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		saturday_open VARCHAR(6) NOT NULL DEFAULT '00:00',
																		saturday_close VARCHAR(6) NOT NULL DEFAULT '00:00',
																		PRIMARY KEY (staffscheduleid),
																		KEY staffschedules1 (staffid)
																	);";

				$_SWIFT["query"]["create"]["departments"] = "CREATE TABLE ". TABLE_PREFIX ."departments(
																		departmentid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		title VARCHAR(100) NOT NULL DEFAULT '',
																		departmenttype ENUM('public', 'private') NOT NULL DEFAULT 'public',
																		departmentmodule ENUM('tickets', 'bugs', 'livesupport', 'knowledgebase') NOT NULL DEFAULT 'tickets',
																		isdefault INT(1) UNSIGNED NOT NULL DEFAULT '0',
																		displayorder INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY(departmentid),
																		KEY departments1 (departmentmodule)
																	);";

				$_SWIFT["query"]["create"]["staffassigns"] = "CREATE TABLE ". TABLE_PREFIX ."staffassigns(
																		staffassignid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		departmentid INT UNSIGNED NOT NULL DEFAULT '0',
																		staffid INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY(staffassignid),
																		KEY staffassigns1 (staffid),
																		KEY staffassigns2 (departmentid),
																		UNIQUE KEY (departmentid, staffid)
																	);";

				$_SWIFT["query"]["create"]["groupassigns"] = "CREATE TABLE ". TABLE_PREFIX ."groupassigns(
																		groupassignid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		departmentid INT UNSIGNED NOT NULL DEFAULT '0',
																		staffgroupid INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY(groupassignid),
																		KEY groupassigns1 (staffgroupid),
																		KEY groupassigns2 (departmentid),
																		UNIQUE KEY (departmentid, staffgroupid)
																	);";

				$_SWIFT["query"]["create"]["templategroup"] = "CREATE TABLE ". TABLE_PREFIX ."templategroups(
																		tgroupid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		languageid INT UNSIGNED NOT NULL DEFAULT '0',
																		guestusergroupid INT UNSIGNED NOT NULL DEFAULT '0',
																		regusergroupid INT UNSIGNED NOT NULL DEFAULT '0',
																		title VARCHAR(60) NOT NULL DEFAULT '',
																		description VARCHAR(255) NOT NULL DEFAULT '',
																		companyname VARCHAR(255) NOT NULL DEFAULT '',
																		ismaster SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		grouppassword VARCHAR(32) NOT NULL DEFAULT '0',
																		restrictgroups SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		isdefault SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		loginapi_moduleid INT UNSIGNED NOT NULL DEFAULT '0',
																		ticketstatusid INT UNSIGNED NOT NULL DEFAULT '0',
																		priorityid INT UNSIGNED NOT NULL DEFAULT '0',
																		departmentid INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY(tgroupid)
																	);";

				$_SWIFT["query"]["create"]["templatecategories"] = "CREATE TABLE ". TABLE_PREFIX ."templatecategories (
																		tcategoryid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		tgroupid INT UNSIGNED NOT NULL DEFAULT '0',
																		name VARCHAR(60) NOT NULL DEFAULT '',
																		description VARCHAR(255) NOT NULL DEFAULT '',
																		module VARCHAR(100) NOT NULL DEFAULT '',
																		PRIMARY KEY(tcategoryid),
																		KEY templatecategories1 (tgroupid)
																	);";

				$_SWIFT["query"]["create"]["template"] = "CREATE TABLE ". TABLE_PREFIX ."templates (
																		templateid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		tgroupid INT UNSIGNED NOT NULL DEFAULT '0',
																		tcategoryid INT UNSIGNED NOT NULL DEFAULT '0',
																		name VARCHAR(60) NOT NULL DEFAULT '',
																		templatelength INT UNSIGNED NOT NULL DEFAULT '0',
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		modified ENUM('user', 'upgrade', 'notmodified') NOT NULL DEFAULT 'notmodified',
																		contentshash VARCHAR(32) NOT NULL DEFAULT '',
																		contentsdefaulthash VARCHAR(32) NOT NULL DEFAULT '',
																		PRIMARY KEY(templateid),
																		KEY templates1 (tgroupid, name),
																		KEY templates2 (tcategoryid)
																	);";

				$_SWIFT["query"]["create"]["templatedata"] = "CREATE TABLE ". TABLE_PREFIX ."templatedata (
																		templatedataid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		templateid INT UNSIGNED NOT NULL DEFAULT '0',
																		contents MEDIUMTEXT,
																		contentsdefault MEDIUMTEXT,
																		PRIMARY KEY(templatedataid),
																		KEY templatedata1 (templateid)
																	);";

				$_SWIFT["query"]["create"]["cron"] = "CREATE TABLE ". TABLE_PREFIX ."cron(
																		cronid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		nextrun INT UNSIGNED NOT NULL DEFAULT '0',
																		lastrun INT UNSIGNED NOT NULL DEFAULT '0',
																		chour INT NOT NULL DEFAULT '0',
																		cminute INT NOT NULL DEFAULT '0',
																		cday INT NOT NULL DEFAULT '0',
																		module VARCHAR(120) NOT NULL DEFAULT '',
																		action VARCHAR(120) NOT NULL DEFAULT '',
																		autorun SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		name VARCHAR(150) NOT NULL DEFAULT '',
																		PRIMARY KEY(cronid)
																	);";

				$_SWIFT["query"]["create"]["cronlog"] = "CREATE TABLE ". TABLE_PREFIX ."cronlogs(
																		cronlogid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		cronid INT UNSIGNED NOT NULL DEFAULT '0',
																		crontitle VARCHAR(255) NOT NULL DEFAULT '',
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		description VARCHAR(255) NOT NULL DEFAULT '',
																		PRIMARY KEY(cronlogid)
																	);";

				$_SWIFT["query"]["create"]["languages"] = "CREATE TABLE ". TABLE_PREFIX ."languages(
																		languageid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		title VARCHAR(100) NOT NULL DEFAULT '',
																		languagecode VARCHAR(20) NOT NULL DEFAULT '',
																		charset VARCHAR(200) NOT NULL DEFAULT '',
																		author VARCHAR(120) NOT NULL DEFAULT '',
																		textdirection ENUM('ltr', 'rtl') NOT NULL DEFAULT 'ltr',
																		ismaster SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		isdefault SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		displayorder INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY(languageid),
																		KEY languages1 (languagecode)
																	);";

				$_SWIFT["query"]["create"]["languagewords"] = "CREATE TABLE ". TABLE_PREFIX ."languagephrases(
																		phraseid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		languageid INT UNSIGNED NOT NULL DEFAULT '0',
																		section VARCHAR(100) NOT NULL DEFAULT '',
																		code VARCHAR(100) NOT NULL DEFAULT '',
																		sectioncode VARCHAR(255) NOT NULL DEFAULT '',
																		contents TEXT,
																		PRIMARY KEY (phraseid),
																		UNIQUE KEY (languageid, code)
																	);";

				$_SWIFT["query"]["create"]["useremails"] = "CREATE TABLE ". TABLE_PREFIX ."useremails (
																		useremailid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		userid INT UNSIGNED NOT NULL DEFAULT '0',
																		email VARCHAR(255) NOT NULL DEFAULT '',
																		isprimary SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY(useremailid),
																		KEY usersemail1 (userid),
																		KEY usersemail2 (email),
																		UNIQUE KEY (email)
																	);";

				$_SWIFT["query"]["create"]["users"] = "CREATE TABLE ". TABLE_PREFIX ."users(
																		userid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		usergroupid INT UNSIGNED NOT NULL DEFAULT '0',
																		fullname VARCHAR(200) NOT NULL DEFAULT '',
																		phone VARCHAR(200) NOT NULL DEFAULT '',
																		userpassword VARCHAR(50) NOT NULL DEFAULT '',
																		userpasswordtxt VARCHAR(50) NOT NULL DEFAULT '',
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		lastvisit INT UNSIGNED NOT NULL DEFAULT '0',
																		lastactivity INT UNSIGNED NOT NULL DEFAULT '0',
																		enabled SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		loginapi_moduleid INT UNSIGNED NOT NULL DEFAULT '1',
																		loginapi_userid VARCHAR(100) NOT NULL DEFAULT '',
																		languageid INT UNSIGNED NOT NULL DEFAULT '0',
																		timezoneoffset VARCHAR(10) NOT NULL DEFAULT '0',
																		enabledst SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		useremailcount INT UNSIGNED NOT NULL DEFAULT '0',
																		allowemail SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		slaplanid INT UNSIGNED NOT NULL DEFAULT '0',
																		slaexpiry INT UNSIGNED NOT NULL DEFAULT '0',
																		ismanager INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY(userid),
																		KEY users1 (usergroupid),
																		KEY users3 (loginapi_moduleid, loginapi_userid),
																		KEY users4 (enabled, dateline)
																	);";

				$_SWIFT["query"]["create"]["userverifyhash"] = "CREATE TABLE ". TABLE_PREFIX ."userverifyhash (
																		userverifyhashid VARCHAR(50) NOT NULL DEFAULT '0',
																		userid INT UNSIGNED NOT NULL DEFAULT '0',
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY(userverifyhashid)
																	);";

				$_SWIFT["query"]["create"]["usergroups"] = "CREATE TABLE ". TABLE_PREFIX ."usergroups (
																		usergroupid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		title VARCHAR(100) NOT NULL DEFAULT '',
																		grouptype ENUM('guest', 'registered', 'custom') NOT NULL DEFAULT 'custom',
																		ismaster SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		slaplanid INT UNSIGNED NOT NULL DEFAULT '0',
																		slaexpiry INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY(usergroupid),
																		KEY usergroups1 (grouptype)
																	);";

				$_SWIFT["query"]["create"]["usergroupsettings"] = "CREATE TABLE ". TABLE_PREFIX ."usergroupsettings(
																		ugroupsettingid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		usergroupid INT UNSIGNED NOT NULL DEFAULT '0',
																		name VARCHAR(100) NOT NULL DEFAULT '',
																		value VARCHAR(255) NOT NULL DEFAULT '',
																		PRIMARY KEY (ugroupsettingid),
																		KEY usergroupsettings1 (usergroupid),
																		UNIQUE KEY (usergroupid, name)
																	);";

				$_SWIFT["query"]["create"]["tgroupassigns"] = "CREATE TABLE ". TABLE_PREFIX ."tgroupassigns (
																		tgroupassignid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		toassignid INT UNSIGNED NOT NULL DEFAULT '0',
																		type SMALLINT UNSIGNED NOT NULL DEFAULT '0',
																		tgroupid INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY(tgroupassignid),
																		KEY tgroupassigns1 (tgroupid, type),
																		KEY tgroupassigns2 (toassignid, type, tgroupid)
																	);";

				$_SWIFT["query"]["create"]["mailqueuedata"] = "CREATE TABLE ". TABLE_PREFIX ."mailqueuedata (
																		mailqueuedataid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		toemail VARCHAR(255) NOT NULL DEFAULT '',
																		fromemail VARCHAR(255) NOT NULL DEFAULT '',
																		fromname VARCHAR(255) NOT NULL DEFAULT '',
																		subject VARCHAR(255) NOT NULL DEFAULT '',
																		datatext MEDIUMTEXT,
																		datahtml MEDIUMTEXT,
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		ishtml SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY(mailqueuedataid)
																	);";

				$_SWIFT["query"]["create"]["customfields"] = "CREATE TABLE ". TABLE_PREFIX ."customfields (
																		customfieldid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		customfieldgroupid INT UNSIGNED NOT NULL DEFAULT '0',
																		title VARCHAR(255) NOT NULL DEFAULT '',
																		fieldtype SMALLINT(2) UNSIGNED NOT NULL DEFAULT '0',
																		fieldname VARCHAR(100) NOT NULL DEFAULT '',
																		defaultvalue VARCHAR(255) NOT NULL DEFAULT '',
																		isrequired SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		usereditable SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		staffeditable SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		regexpvalidate VARCHAR(255) NOT NULL DEFAULT '',
																		displayorder INT UNSIGNED NOT NULL DEFAULT '0',
																		description VARCHAR(255) NOT NULL DEFAULT '',
																		PRIMARY KEY(customfieldid),
																		KEY customfields1 (customfieldgroupid)
																	);";

				$_SWIFT["query"]["create"]["customfieldgroups"] = "CREATE TABLE ". TABLE_PREFIX ."customfieldgroups (
																		customfieldgroupid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		title VARCHAR(200) NOT NULL DEFAULT '',
																		grouptype SMALLINT(2) UNSIGNED NOT NULL DEFAULT '0',
																		displayorder INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY(customfieldgroupid),
																		KEY customfieldgroups1 (grouptype)
																	);";

				$_SWIFT["query"]["create"]["customfieldvalues"] = "CREATE TABLE ". TABLE_PREFIX ."customfieldvalues (
																		customfieldvalueid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		customfieldid INT UNSIGNED NOT NULL DEFAULT '0',
																		typeid INT UNSIGNED NOT NULL DEFAULT '0',
																		fieldvalue VARCHAR(255) NOT NULL DEFAULT '',
																		isserialized SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY(customfieldvalueid),
																		KEY customfieldvalues1 (customfieldid, typeid)
																	);";

				$_SWIFT["query"]["create"]["customfieldoptions"] = "CREATE TABLE ". TABLE_PREFIX ."customfieldoptions (
																		customfieldoptionid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		customfieldid INT UNSIGNED NOT NULL DEFAULT '0',
																		optionvalue VARCHAR(255) NOT NULL DEFAULT '',
																		displayorder INT UNSIGNED NOT NULL DEFAULT '0',
																		isselected SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY(customfieldoptionid),
																		KEY customfieldoptions1 (customfieldid)
																	);";

				$_SWIFT["query"]["create"]["customfieldlinks"] = "CREATE TABLE ". TABLE_PREFIX ."customfieldlinks (
																		customfieldlinkid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		linktype SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		typeid INT UNSIGNED NOT NULL DEFAULT '0',
																		customfieldgroupid INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY (customfieldlinkid),
																		UNIQUE KEY (linktype, typeid, customfieldgroupid)
																	);";

				$_SWIFT["query"]["create"]["customfielddeplinks"] = "CREATE TABLE ". TABLE_PREFIX ."customfielddeplinks (
																		customfieldgroupid INT UNSIGNED NOT NULL DEFAULT '0',
																		departmentid INT UNSIGNED NOT NULL DEFAULT '0',
																		UNIQUE KEY (customfieldgroupid, departmentid)
																	);";

				$_SWIFT["query"]["create"]["comments"] = "CREATE TABLE ". TABLE_PREFIX ."comments (
																		commentid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		typeid INT UNSIGNED NOT NULL DEFAULT '0',
																		commenttype SMALLINT(2) UNSIGNED NOT NULL DEFAULT '0',
																		fullname VARCHAR(255) NOT NULL DEFAULT '',
																		email VARCHAR(255) NOT NULL DEFAULT '',
																		isapproved SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY (commentid),
																		KEY comments1 (commenttype, isapproved, typeid)
																	);";

				$_SWIFT["query"]["create"]["commentdata"] = "CREATE TABLE ". TABLE_PREFIX ."commentdata (
																		commentdataid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		commentid INT UNSIGNED NOT NULL DEFAULT '0',
																		contents MEDIUMTEXT,
																		PRIMARY KEY (commentdataid),
																		KEY commentdata1 (commentid)
																	);";

				$_SWIFT["query"]["create"]["privatemessages"] = "CREATE TABLE ". TABLE_PREFIX ."privatemessages (
																		privatemessageid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		fromstaffid INT UNSIGNED NOT NULL DEFAULT '0',
																		tostaffid INT UNSIGNED NOT NULL DEFAULT '0',
																		subject VARCHAR(255) NOT NULL DEFAULT '',
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		msgtype SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		msgstatus SMALLINT(1) UNSIGNED NOT NULL DEFAULT '0',
																		readdate INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY (privatemessageid),
																		KEY privatemessages1 (tostaffid, msgtype, msgstatus),
																		KEY privatemessages2 (fromstaffid, msgtype, msgstatus)
																	);";

				$_SWIFT["query"]["create"]["privatemessagedata"] = "CREATE TABLE ". TABLE_PREFIX ."privatemessagedata (
																		privatemessagedataid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		privatemessageid INT UNSIGNED NOT NULL DEFAULT '0',
																		contents MEDIUMTEXT,
																		PRIMARY KEY (privatemessagedataid),
																		KEY privatemessagedata1 (privatemessageid)
																	);";

				$_SWIFT["query"]["create"]["tempfiles"] = "CREATE TABLE ". TABLE_PREFIX ."tempfiles (
																		tempfileid INT UNSIGNED NOT NULL AUTO_INCREMENT,
																		filename VARCHAR(255) NOT NULL DEFAULT '',
																		dateline INT UNSIGNED NOT NULL DEFAULT '0',
																		PRIMARY KEY (tempfileid)
																	);";


				// $_SWIFT["query"]["alter"]["session"][0] = "ALTER TABLE `". TABLE_PREFIX ."sessions` TYPE = HEAP;";
				$_SWIFT["query"]["alter"]["session"][0] = "use mscc;";

				$_SWIFT["query"]["insert"]["staff"][0] = "INSERT INTO `". TABLE_PREFIX ."staff` (`fullname`, `username`, `password`, `staffgroupid`, `email`, `lastvisit`, `lastactivity`, `timezoneoffset`, `enabledst`, `startofweek`, `pmunread`, `groupassigns`, `enablepmalerts`, `enablepmjsalerts`) VALUES('". addslashes($_POST["fullname"]) ."', '". addslashes($_POST["username"]) ."', '". md5($_POST["password"]) ."', '1', '". addslashes($_POST["email"]) ."', '". DATENOW ."', '". DATENOW ."', '99', '0', '1', '0', '0', '1', '1');";

				$_SWIFT["query"]["insert"]["staffschedules"][0] = "INSERT INTO `".TABLE_PREFIX ."staffschedules` (`staffid`, `sunday_enabled`, `sunday_open`, `sunday_close`, `monday_enabled`, `monday_open`, `monday_close`, `tuesday_enabled`, `tuesday_open`, `tuesday_close`, `wednesday_enabled`, `wednesday_open`, `wednesday_close`, `thursday_enabled`, `thursday_open`, `thursday_close`, `friday_enabled`, `friday_open`, `friday_close`, `saturday_enabled`, `saturday_open`, `saturday_close`) VALUES ('1', '0', '00:00', '23:45', '1', '08:00', '19:00', '1', '08:00', '19:00', '1', '08:00', '19:00', '1', '08:00', '19:00', '1', '08:00', '19:00', '0', '00:00', '23:45');";

				$_SWIFT["query"]["insert"]["signatures"][] = "INSERT INTO `". TABLE_PREFIX ."signatures` (`dateline`, `staffid`, `signature`) VALUES('". time() ."', '1', '');";

				$_SWIFT["query"]["insert"]["staffgroup"][] = "INSERT INTO `". TABLE_PREFIX ."staffgroup` (`title`, `isadmin`) VALUES('Administrator', '1');";
				$_SWIFT["query"]["insert"]["staffgroup"][] = "INSERT INTO `". TABLE_PREFIX ."staffgroup` (`title`, `isadmin`) VALUES('Staff', '0');";
				
				$_SWIFT["query"]["insert"]["groupassigns"][] = "INSERT INTO `". TABLE_PREFIX ."groupassigns` (`departmentid`, `staffgroupid`) VALUES('1', '1');";
				$_SWIFT["query"]["insert"]["groupassigns"][] = "INSERT INTO `". TABLE_PREFIX ."groupassigns` (`departmentid`, `staffgroupid`) VALUES('1', '2');";

				$_SWIFT["query"]["insert"]["staffassigns"][] = "INSERT INTO `". TABLE_PREFIX ."staffassigns` (`departmentid`, `staffid`) VALUES('1', '1');";
				
				$_SWIFT["query"]["insert"]["tgroupassigns"][] = "INSERT INTO `". TABLE_PREFIX ."tgroupassigns` (`toassignid`, `type`, `tgroupid`) VALUES('1', '". TGROUP_DEPARTMENT ."', '1');";

				if (SWIFTPRODUCT == PRODUCT_SUPPORTSUITE)
				{
					$_SWIFT["query"]["insert"]["departments"][] = "INSERT INTO `". TABLE_PREFIX ."departments` (`title`, `departmenttype`, `departmentmodule`, `isdefault`, `displayorder`) VALUES('General', 'public', '". MODULE_TICKETS ."', '1', '0');";
					$_SWIFT["query"]["insert"]["departments"][] = "INSERT INTO `". TABLE_PREFIX ."departments` (`title`, `departmenttype`, `departmentmodule`, `isdefault`, `displayorder`) VALUES('General', 'public', '". MODULE_LIVESUPPORT ."', '0', '1');";

					$_SWIFT["query"]["insert"]["staffassigns"][] = "INSERT INTO `". TABLE_PREFIX ."staffassigns` (`departmentid`, `staffid`) VALUES('2', '1');";
					
					$_SWIFT["query"]["insert"]["groupassigns"][] = "INSERT INTO `". TABLE_PREFIX ."groupassigns` (`departmentid`, `staffgroupid`) VALUES('2', '1');";
					$_SWIFT["query"]["insert"]["groupassigns"][] = "INSERT INTO `". TABLE_PREFIX ."groupassigns` (`departmentid`, `staffgroupid`) VALUES('2', '2');";

					$_SWIFT["query"]["insert"]["tgroupassigns"][] = "INSERT INTO `". TABLE_PREFIX ."tgroupassigns` (`toassignid`, `type`, `tgroupid`) VALUES('2', '". TGROUP_DEPARTMENT ."', '1');";

				} else if (SWIFTPRODUCT == PRODUCT_ESUPPORT) {
					$_SWIFT["query"]["insert"]["departments"][] = "INSERT INTO `". TABLE_PREFIX ."departments` (`title`, `departmenttype`, `departmentmodule`, `isdefault`, `displayorder`) VALUES('General', 'public', '". MODULE_TICKETS ."', '1', '0');";
				} else if (SWIFTPRODUCT == PRODUCT_LIVERESPONSE) {
					$_SWIFT["query"]["insert"]["departments"][] = "INSERT INTO `". TABLE_PREFIX ."departments` (`title`, `departmenttype`, `departmentmodule`, `isdefault`, `displayorder`) VALUES('General', 'public', '". MODULE_LIVESUPPORT ."', '1', '0');";
				} else if (SWIFTPRODUCT == PRODUCT_HELPBASE) {
					$_SWIFT["query"]["insert"]["departments"][] = "INSERT INTO `". TABLE_PREFIX ."departments` (`title`, `departmenttype`, `departmentmodule`, `isdefault`, `displayorder`) VALUES('General', 'public', '". MODULE_KNOWLEDGEBASE ."', '1', '0');";
				}



				$_SWIFT["query"]["insert"]["settings"][] = "INSERT INTO `". TABLE_PREFIX ."settings` (`section`, `vkey`, `data`) VALUES('core', 'version', '". addslashes(SWIFTVERSION) ."')";
				$_SWIFT["query"]["insert"]["settings"][] = "INSERT INTO `". TABLE_PREFIX ."settings` (`section`, `vkey`, `data`) VALUES('core', 'product', '". addslashes(SWIFTPRODUCT) ."')";
				$_SWIFT["query"]["insert"]["settings"][] = "INSERT INTO `". TABLE_PREFIX ."settings` (`section`, `vkey`, `data`) VALUES('cron', 'nextrun', '". addslashes(time()) ."')";

				// Build File IDs
				$_kbcache = buildHash().".js";
				$_jscache = buildHash().".js";
				$_SWIFT["query"]["insert"]["settings"][] = "INSERT INTO `". TABLE_PREFIX ."settings` (`section`, `vkey`, `data`) VALUES('fileid', 'knowledgebase', '". addslashes($_kbcache) ."')";
				$_SWIFT["query"]["insert"]["settings"][] = "INSERT INTO `". TABLE_PREFIX ."settings` (`section`, `vkey`, `data`) VALUES('fileid', 'jscachefile', '". addslashes($_jscache) ."')";

				@touch("./files/".$_jscache);
				@touch("./files/".$_kbcache);

				$_SWIFT["query"]["insert"]["usergroups"][] = "INSERT INTO `". TABLE_PREFIX ."usergroups` (`title`, `grouptype`, `ismaster`) VALUES ('Guest', 'guest', '1');";
				$_SWIFT["query"]["insert"]["usergroups"][] = "INSERT INTO `". TABLE_PREFIX ."usergroups` (`title`, `grouptype`, `ismaster`) VALUES ('Registered', 'registered', '1');";

				$_SWIFT["query"]["insert"]["templategroups"][] = "INSERT INTO `". TABLE_PREFIX ."templategroups` (`languageid`, `guestusergroupid`, `regusergroupid`, `title`, `description`, `companyname`, `ismaster`, `grouppassword`, `restrictgroups`, `isdefault`, `loginapi_moduleid`, `ticketstatusid`, `priorityid`, `departmentid`) VALUES('1', '1', '2', 'default', 'Default Template Group', '". addslashes($_POST["companyname"]) ."', '1', '', '0', '1', '". LOGINAPI_DEFAULT ."', '1', '1', '1');";

				$_SWIFT["query"]["insert"]["cron"][] = "INSERT INTO `". TABLE_PREFIX ."cron` (`nextrun`, `lastrun`, `chour`, `cminute`, `cday`, `module`, `action`, `autorun`, `name`) VALUES ('". time() ."', '". time() ."', '-1', '0', '0', 'core', 'hourlycleanup', '1', 'cronhourlycleanup');";
				$_SWIFT["query"]["insert"]["cron"][] = "INSERT INTO `". TABLE_PREFIX ."cron` (`nextrun`, `lastrun`, `chour`, `cminute`, `cday`, `module`, `action`, `autorun`, `name`) VALUES ('". time() ."', '". time() ."', '0', '0', '-1', 'core', 'dailycleanup', '1', 'crondailycleanup');";
				execAllQueries();

				/**
				* ###############################################
				* BUILD ALL CACHES
				* ###############################################
				*/

				// ======= STAFF CACHE =======
				rebuildStaffCache();

				// ======= STAFF GROUP CACHE =======
				rebuildStaffGroupCache();

				// ======= DEPARTMENT CACHE =======
				rebuildDepartmentCache();

				// ======= GROUP ASSIGNS =======
				rebuildGroupAssignCache();

				// ======= STAFF ASSIGNS =======
				rebuildStaffAssignCache();

				break;
		}

	}
}
?>